<template>
	<view>
		<view class="wrap">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#d31812" ref="tabs" :list="list" :current="current" @change="change"
					:is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition"
				@animationfinish="animationfinish">
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(res, index) in orderAll" :key="res.id">
								<view class="top" @click="openPage('/pages/ucp/order-detail',{id:res.id})">
									<view class="left">
										<u-icon name="order" :size="30" color="rgb(94,94,94)"></u-icon>
										<view class="store">{{ res.serial_code }}</view>
										<!-- <u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon> -->
									</view>
									<view class="right">{{ stateDesc(res) }}</view>
								</view>
								<u-line></u-line>
								<view class="item" v-for="(item, index) in res.goods_list" :key="index" @click="openPage('/pages/buy/item',{id:item.item_id})">
									<view class="left">
										<image :src="item.image" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2"><view v-if="item.is_gift" class="gift-tag">赠品</view>{{ item.item_name }}</view>
										<view class="type">{{ item.item_remark }}</view>
										<!-- <view class="delivery-time">发货时间 {{ item.deliveryTime }}</view> -->
									</view>
									<view class="right">
										<view class="price" :class="{'line-through':item.is_gift===1}">
											￥{{ item.unit_price }}
										</view>
										<view class="number">x{{ item.quantity }}</view>
									</view>
								</view>
								<u-line class="u-p-t-20"></u-line>
								<view class="bottom">
									<view class="u-flex-1">
										共{{ totalNum(res.goods_list) }}件商品 合计:
										<text class="total-price">
											￥{{ res.total_amount }}</text>
										</text>
									</view>
									<view class="logistics btn"
										@click="openPage('/pages/ucp/order-detail',{id:res.id})">订单详情</view>
									<!-- <view class="evaluate btn">评价</view> -->
								</view>
							</view>
							<view v-if="orderAll.length===0">
								<view class="centre">
									<image src="https://mafu.oss-cn-shenzhen.aliyuncs.com/shop-user/202509/oss-2b4da70324a6838cecc364e40bdfd9be.png" mode="">
									</image>
									<view class="explain">
										您还没有相关的订单
										<view class="tips">可以去看看有那些想买的</view>
									</view>
									<view class="btn" @click="toHome">随便逛逛</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[0]"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
				
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(res, index) in orderPendingPay" :key="res.id">
								<view class="top" @click="openPage('/pages/ucp/order-detail',{id:res.id})">
									<view class="left">
										<u-icon name="order" :size="30" color="rgb(94,94,94)"></u-icon>
										<view class="store">{{ res.serial_code }}</view>
										<!-- <u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon> -->
									</view>
									<view class="right">{{ stateDesc(res) }}</view>
								</view>
								<u-line></u-line>
								<view class="item" v-for="(item, index) in res.goods_list" :key="index" @click="openPage('/pages/buy/item',{id:item.item_id})">
									<view class="left">
										<image :src="item.image" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2"><view v-if="item.is_gift" class="gift-tag">赠品</view>{{ item.item_name }}</view>
										<view class="type">{{ item.item_remark }}</view>
										<!-- <view class="delivery-time">发货时间 {{ item.deliveryTime }}</view> -->
									</view>
									<view class="right">
										<view class="price" :class="{'line-through':item.is_gift===1}">
											￥{{ item.unit_price }}
										</view>
										<view class="number">x{{ item.quantity }}</view>
									</view>
								</view>
								<u-line class="u-p-t-20"></u-line>
								<view class="bottom">
									<view class="u-flex-1">
										共{{ totalNum(res.goods_list) }}件商品 合计:
										<text class="total-price">
											￥{{ res.total_amount }}</text>
										</text>
									</view>
									<view class="logistics btn"
										@click="openPage('/pages/ucp/order-detail',{id:res.id})">订单详情</view>
									<!-- <view class="evaluate btn">评价</view> -->
								</view>
							</view>
							<view v-if="orderPendingPay.length===0">
								<view class="centre">
									<image src="https://mafu.oss-cn-shenzhen.aliyuncs.com/shop-user/202509/oss-2b4da70324a6838cecc364e40bdfd9be.png" mode="">
									</image>
									<view class="explain">
										您还没有相关的订单
										<view class="tips">可以去看看有那些想买的</view>
									</view>
									<view class="btn" @click="toHome">随便逛逛</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[1]"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
				
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;">
						<view class="page-box">
							<view class="order" v-for="(res, index) in orderPendingSend" :key="res.id">
								<view class="top" @click="openPage('/pages/ucp/order-detail',{id:res.id})">
									<view class="left">
										<u-icon name="order" :size="30" color="rgb(94,94,94)"></u-icon>
										<view class="store">{{ res.serial_code }}</view>
										<!-- <u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon> -->
									</view>
									<view class="right">{{ stateDesc(res) }}</view>
								</view>
								<u-line></u-line>
								<view class="item" v-for="(item, index) in res.goods_list" :key="index" @click="openPage('/pages/buy/item',{id:item.item_id})">
									<view class="left">
										<image :src="item.image" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2"><view v-if="item.is_gift" class="gift-tag">赠品</view>{{ item.item_name }}</view>
										<view class="type">{{ item.item_remark }}</view>
										<!-- <view class="delivery-time">发货时间 {{ item.deliveryTime }}</view> -->
									</view>
									<view class="right">
										<view class="price" :class="{'line-through':item.is_gift===1}">
											￥{{ item.unit_price }}
										</view>
										<view class="number">x{{ item.quantity }}</view>
									</view>
								</view>
								<u-line class="u-p-t-20"></u-line>
								<view class="bottom">
									<view class="u-flex-1">
										共{{ totalNum(res.goods_list) }}件商品 合计:
										<text class="total-price">
											￥{{ res.total_amount }}</text>
										</text>
									</view>
									<view class="logistics btn"
										@click="openPage('/pages/ucp/order-detail',{id:res.id})">订单详情</view>
									<!-- <view class="evaluate btn">评价</view> -->
								</view>
							</view>
							<view v-if="orderPendingSend.length===0">
								<view class="centre">
									<image src="https://mafu.oss-cn-shenzhen.aliyuncs.com/shop-user/202509/oss-2b4da70324a6838cecc364e40bdfd9be.png" mode="">
									</image>
									<view class="explain">
										您还没有相关的订单
										<view class="tips">可以去看看有那些想买的</view>
									</view>
									<view class="btn" @click="toHome">随便逛逛</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[2]"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
				
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(res, index) in orderPendingReceive" :key="res.id">
								<view class="top" @click="openPage('/pages/ucp/order-detail',{id:res.id})">
									<view class="left">
										<u-icon name="order" :size="30" color="rgb(94,94,94)"></u-icon>
										<view class="store">{{ res.serial_code }}</view>
										<!-- <u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon> -->
									</view>
									<view class="right">{{ stateDesc(res) }}</view>
								</view>
								<u-line></u-line>
								<view class="item" v-for="(item, index) in res.goods_list" :key="index" @click="openPage('/pages/buy/item',{id:item.item_id})">
									<view class="left">
										<image :src="item.image" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2"><view v-if="item.is_gift" class="gift-tag">赠品</view>{{ item.item_name }}</view>
										<view class="type">{{ item.item_remark }}</view>
										<!-- <view class="delivery-time">发货时间 {{ item.deliveryTime }}</view> -->
									</view>
									<view class="right">
										<view class="price" :class="{'line-through':item.is_gift===1}">
											￥{{ item.unit_price }}
										</view>
										<view class="number">x{{ item.quantity }}</view>
									</view>
								</view>
								<u-line class="u-p-t-20"></u-line>
								<view class="bottom">
									<view class="u-flex-1">
										共{{ totalNum(res.goods_list) }}件商品 合计:
										<text class="total-price">
											￥{{ res.total_amount }}</text>
										</text>
									</view>
									<view class="logistics btn"
										@click="openPage('/pages/ucp/order-detail',{id:res.id})">订单详情</view>
									<!-- <view class="evaluate btn">评价</view> -->
								</view>
							</view>
							<view v-if="orderPendingReceive.length===0">
								<view class="centre">
									<image src="https://mafu.oss-cn-shenzhen.aliyuncs.com/shop-user/202509/oss-2b4da70324a6838cecc364e40bdfd9be.png" mode="">
									</image>
									<view class="explain">
										您还没有相关的订单
										<view class="tips">可以去看看有那些想买的</view>
									</view>
									<view class="btn" @click="toHome">随便逛逛</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[3]"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
							
				
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(res, index) in orderPendingRedeem" :key="res.id">
								<view class="top" @click="openPage('/pages/ucp/order-detail',{id:res.id})">
									<view class="left">
										<u-icon name="order" :size="30" color="rgb(94,94,94)"></u-icon>
										<view class="store">{{ res.serial_code }}</view>
										<!-- <u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon> -->
									</view>
									<view class="right">{{ stateDesc(res) }}</view>
								</view>
								<u-line></u-line>
								<view class="item" v-for="(item, index) in res.goods_list" :key="index" @click="openPage('/pages/buy/item',{id:item.item_id})">
									<view class="left">
										<image :src="item.image" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2"><view v-if="item.is_gift" class="gift-tag">赠品</view>{{ item.item_name }}</view>
										<view class="type">{{ item.item_remark }}</view>
										<!-- <view class="delivery-time">发货时间 {{ item.deliveryTime }}</view> -->
									</view>
									<view class="right">
										<view class="price" :class="{'line-through':item.is_gift===1}">
											￥{{ item.unit_price }}
										</view>
										<view class="number">x{{ item.quantity }}</view>
									</view>
								</view>
								<u-line class="u-p-t-20"></u-line>
								<view class="bottom">
									<view class="u-flex-1">
										共{{ totalNum(res.goods_list) }}件商品 合计:
										<text class="total-price">
											￥{{ res.total_amount }}</text>
										</text>
									</view>
									<view class="logistics btn"
										@click="openPage('/pages/ucp/order-detail',{id:res.id})">订单详情</view>
									<!-- <view class="evaluate btn">评价</view> -->
								</view>
							</view>
							<view v-if="orderPendingRedeem.length===0">
								<view class="centre">
									<image src="https://mafu.oss-cn-shenzhen.aliyuncs.com/shop-user/202509/oss-2b4da70324a6838cecc364e40bdfd9be.png" mode="">
									</image>
									<view class="explain">
										您还没有相关的订单
										<view class="tips">可以去看看有那些想买的</view>
									</view>
									<view class="btn" @click="toHome">随便逛逛</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[4]"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
			
			</swiper>
		</view>
		<wx-share :title='shop.name' :desc="shop.store_introduction" :imgUrl="shop.cover" link="/" ref="wxs"></wx-share>
		<custom-tabbar></custom-tabbar>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				orderList: [
					[],
					[],
					[],
					[],
					[]
				],
				paging: [{
					current_page: 1,
					total_pages: 1,
					page_size: 50
				}, {
					current_page: 1,
					total_pages: 1,
					page_size: 50
				}, {
					current_page: 1,
					total_pages: 1,
					page_size: 50
				}, {
					current_page: 1,
					total_pages: 1,
					page_size: 50
				},{
					current_page: 1,
					total_pages: 1,
					page_size: 50
				}],
				list: [{
						name: '全部订单'
					},
					{
						name: '待付款',
						count: 0
					},
					{
						name: '待发货',
						count: 0
					},
					{
						name: '待收货',
						count: 0
					},
					{
						name: '待使用',
						count: 0
					}
				],
				current: 0,
				swiperCurrent: 0,
				tabsHeight: 0,
				dx: 0,
				loadStatus: ['loadmore', 'loadmore', 'loadmore', 'loadmore','loadmore'],
				orderAll:[],
				orderPendingPay:[],
				orderPendingSend:[],
				orderPendingReceive:[],
				orderPendingRedeem:[],
				shop:{}
			};
		},
		onLoad(options) {
			if (options.state === 'pending_pay') {
				this.current = 1;
			} else if (options.state === 'pending_send') {
				this.current = 2;
			} else if (options.state === 'pending_receive') {
				this.current = 3;
			}else if(options.state==='pending_redeem'){
				this.current = 4;
			}
			this.swiperCurrent = this.current;
			this.getOrderList(0);
			this.getOrderList(1);
			this.getOrderList(2);
			this.getOrderList(3);
			this.getOrderList(4);
			this.getOrderSummary();
			this.getShop();
		},
		onShow() {

		},
		computed: {

		},
		methods: {
			getShop() {
				this.$u.post('/shopping/shop-setting', {}).then(res => {
					this.shop = res;
				}).catch(e => {

				})
			},
			stateDesc(payment){
				if(payment.state==='pending_pay'){
					return '待支付'
				}
				if(payment.state==='paid'){
					if(payment.delivery_state==='complete'){
						return '已完成'
					}
					if(payment.delivery_state==='store_handling'){
						return '商家捡货中'
					}
					if(payment.delivery_state==='pending_receive'){
						return '商家配送中'
					}
					if(payment.delivery_state==='pending_pickup'){
						return '待自提'
					}
					return '';
				}
				if(payment.state==='pending_refund'){
					return '退款处理中';
				}
				if(payment.state==='refunded'){
					return '已退款';
				}
				if(payment.state==='expired'){
					return '订单已失效';
				}
			},
			toHome(){
				uni.switchTab({
					url:'/pages/index/index'
				})
			},
			openPage(jumpURL, params = {}) {
				this.$u.route({
					type: 'to',
					params: params,
					url: jumpURL,
					animationType: 'slide-in-bottom'
				});
			},
			getOrderSummary() {
				this.$u.post('/payment/order-summary', {}).then(res => {
					this.list[1].count = res.pending_pay;
					this.list[2].count = res.pending_send;
					this.list[3].count = res.pending_receive;
					this.list[4].count = res.pending_redeem;
				}).catch(e => {

				})
			},
			reachBottom() {
				// 此tab为空数据

					this.loadStatus.splice(this.current, 1, "loading")
					setTimeout(() => {
						this.paging[this.current].current_page = ++this.paging[this.current].current_page;
						this.getOrderList(this.current);
					}, 1200);

			},
			// 页面数据
			getOrderList(idx) {
				if (idx === 0) {
					this.$u.post('/payment/payment-list', {
						state: '',
						page_size: this.paging[idx].page_size,
						current_page: this.paging[idx].current_page
					}).then(res => {
						if (res.page.current_page === 1) {
							this.orderAll = res.list;
						} else {
							this.orderAll = this.orderAll.concat(res.list);
						}
						this.paging[idx].current_page = res.page.current_page;
						this.paging[idx].total_pages = res.page.total_pages;
						this.paging[idx].page_size = res.data.page_size;
					}).catch(e => {
					})
				}else if(idx ===1){
					this.$u.post('/payment/payment-list', {
						state: 'pending_pay',
						page_size: this.paging[idx].page_size,
						current_page: this.paging[idx].current_page
					}).then(res => {
						if (res.page.current_page === 1) {
							this.orderPendingPay = res.list;
						} else {
							this.orderPendingPay = this.orderPendingPay.concat(res.list);
						}
						this.paging[idx].current_page = res.page.current_page;
						this.paging[idx].total_pages = res.page.total_pages;
						this.paging[idx].page_size = res.data.page_size;
					}).catch(e => {
					})
				}else if(idx===2){
					this.$u.post('/payment/payment-list', {
						state: 'pending_send',
						page_size: this.paging[idx].page_size,
						current_page: this.paging[idx].current_page
					}).then(res => {
						if (res.page.current_page === 1) {
							this.orderPendingSend = res.list;
						} else {
							this.orderPendingSend = this.orderPendingSend.concat(res.list);
						}
						this.paging[idx].current_page = res.page.current_page;
						this.paging[idx].total_pages = res.page.total_pages;
						this.paging[idx].page_size = res.data.page_size;
					}).catch(e => {
					})
				}else if(idx === 3){
					this.$u.post('/payment/payment-list', {
						state: 'pending_receive',
						page_size: this.paging[idx].page_size,
						current_page: this.paging[idx].current_page
					}).then(res => {
						if (res.page.current_page === 1) {
							this.orderPendingReceive = res.list;
						} else {
							this.orderPendingReceive = this.orderPendingReceive.concat(res.list);
						}
						this.paging[idx].current_page = res.page.current_page;
						this.paging[idx].total_pages = res.page.total_pages;
						this.paging[idx].page_size = res.data.page_size;
					}).catch(e => {
					})
				} else if(idx === 4){
					this.$u.post('/payment/payment-list', {
						state: 'pending_redeem',
						page_size: this.paging[idx].page_size,
						current_page: this.paging[idx].current_page
					}).then(res => {
						if (res.page.current_page === 1) {
							this.orderPendingRedeem = res.list;
						} else {
							this.orderPendingRedeem = this.orderPendingRedeem.concat(res.list);
						}
						this.paging[idx].current_page = res.page.current_page;
						this.paging[idx].total_pages = res.page.total_pages;
						this.paging[idx].page_size = res.data.page_size;
					}).catch(e => {
					})
				}
				this.loadStatus.splice(this.current, 1, "loadmore")
			},
			// 总件数
			totalNum(item) {
				let num = 0;
				item.map(val => {
					num += val.quantity;
				});
				return num;
			},
			// tab栏切换
			change(index) {
				this.swiperCurrent = index;
				// this.getOrderList(index);
			},
			transition({
				detail: {
					dx
				}
			}) {
				this.$refs.tabs.setDx(dx);
			},
			animationfinish({
				detail: {
					current
				}
			}) {
				this.$refs.tabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;
			}
		}
	};
</script>

<style>
	/* #ifndef H5 */
	page {
		height: 100%;
		background-color: #f2f2f2;
	}

	/* #endif */
</style>

<style lang="scss" scoped>
	.gift-tag{
		display:inline-block;
		background-color: red;
		color: #fff;
		border-radius: 4px;
		font-size: .8em;
		padding: 0 10rpx;
		line-height: 1.5;
		margin-right: .5em;
	}
	.line-through{
		text-decoration: line-through;
	}
	
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;
			padding-top: 20rpx;
			padding-bottom:30rpx;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-type-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 140rpx;
					height: 140rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				width:100%;
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				// padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 20rpx;
			padding: 0 10rpx;
			justify-content: flex-end;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
				margin-left: 26rpx;
			}

			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 320rpx;
			height: 320rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(209, 8, 1, 0.8) 20%, rgba(209, 8, 1, 0.4) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
</style>
